package com.sita.tboard.roadtrust.detail;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

import com.sita.bike.R;
import com.sita.bike.model.EaseUser2;
import com.sita.bike.persistence.RtNoteEntityDao;
import com.sita.bike.persistence.TrendMember;
import com.sita.bike.support.GlobalContext;
import com.sita.bike.utils.PersistUtils;
import com.sita.friend.FriendHelper;
import com.sita.tboard.util.DateUtils;
import com.sita.tboard.view.RecyclerViewCursorAdapter;
import com.sita.tboard.view.RecyclerViewCursorViewHolder;
import com.squareup.picasso.Picasso;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by xiaodong on 16/3/28.
 */
public class RtNoteListAdapter  extends RecyclerViewCursorAdapter<RtNoteListAdapter.RtNoteListViewHolder> {
    /**
     * Constructor.
     *
     * @param context The Context the Adapter is displayed in.
     */
    protected RtNoteListAdapter(Context context) {
        super(context);
        setupCursorAdapter(null, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER, R.layout.item_rt_note_in_list, false);
    }

    @Override
    public RtNoteListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.item_rt_note_in_list, parent, false);
        return new RtNoteListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RtNoteListViewHolder holder, int position) {
        // Move cursor to this position
        mCursorAdapter.getCursor().moveToPosition(position);

        // Set the ViewHolder
        setViewHolder(holder);

        // Bind this view
        mCursorAdapter.bindView(null, mContext, mCursorAdapter.getCursor());
    }

    public static class RtNoteListViewHolder extends RecyclerViewCursorViewHolder {

        CircleImageView avatarView;
        TextView nicknameView;
        TextView timeView;
        TextView messageView;

        public RtNoteListViewHolder(View itemView) {
            super(itemView);
            avatarView = (CircleImageView)itemView.findViewById(R.id.avatar);
            nicknameView = (TextView)itemView.findViewById(R.id.nickname);
            timeView = (TextView)itemView.findViewById(R.id.time);
            messageView = (TextView)itemView.findViewById(R.id.message);
        }

        public void bindCursor(Cursor mCursor) {
            Long noteId = mCursor.getLong(mCursor.getColumnIndex(RtNoteEntityDao.Properties.Id.columnName));
            String accountId = mCursor.getString(mCursor.getColumnIndex(RtNoteEntityDao.Properties.AccountId.columnName));
            String message = mCursor.getString(mCursor.getColumnIndex(RtNoteEntityDao.Properties.Message.columnName));
            long createTime = mCursor.getLong(mCursor.getColumnIndex(RtNoteEntityDao.Properties.CreateTime.columnName));
            String time = DateUtils.translateDateTime(createTime);
            timeView.setText(time);

            messageView.setText(message);

            fillAccount(accountId);
        }

        private void fillAccount(String accountId) {
            String avatar = null;
            String nickname = null;
            EaseUser2 friend = FriendHelper.getInstance().getContactUser(accountId);
            if (friend != null) {
                avatar = friend.getAvatar();
                nickname = friend.getUsername().equals("") ? friend.getNick() : friend.getUsername();
            } else {
                // 非好友
                TrendMember member = PersistUtils.getTrendMember(accountId);
                if (member != null) {
                    avatar = member.getAvatarUrl();
                    nickname = member.getNickname();
                }
            }
            Picasso.with(GlobalContext.getGlobalContext()).load(avatar).fit()
                    .placeholder(R.drawable.default_head)
                    .centerInside().into(avatarView);
            nicknameView.setText(nickname);
        }
    }
}
